// 新科室 
import React from 'react';
import { Tabs, Row, Col, Divider, Icon, message } from 'antd';
import store from '../../store';
import { $http } from '../../containers/config/https';
import DepartmentBasicInformation from './common/DepartmentBasicInformation.jsx'; // 科室基本信息
import DoctorList from './common/DoctorList.jsx'; // 坐诊
import DoctorScheduling from './common/DoctorScheduling.jsx'; // 医生排班
import WardContrst from './common/WardContrast.jsx';//病历对照
import ExternalDepartments from './common/ExternalDepartments.jsx';//向外院开单项目目录
import DepartmentExpertise from './common/DepartmentExpertise.jsx';//科室专长维护
import DepartmentFind from './common/DepartmentFind.jsx';//科室床位
import OfficeStaff from './common/OfficeStaff.jsx';//科室人员
import DepartmentContrastDoctor from './common/DepartmentContrastDoctor.jsx';//科室挂号医生对照
// import Office from './common/Office'
// import OfficeStaffEdit from './common/OfficeStaffEdit.jsx';
import WardInformationSettings from 'pages/nurseStation/WardInformationSettings.jsx'; // 病区信息设置
import './style/index.less';

const TabPane = Tabs.TabPane;

export default class NewDepartment extends React.Component {
	constructor({ poprs }) {
		super(poprs);
		this.state = {
			activeKey: '1',
			userInfo: {},
			locID: '',
			locTypeCode: '', // 科室类型
		}
	}

	componentDidMount() {
		if (this.props.paras) {
			let { flag, userInfo } = this.props.paras;
			if (flag === 'add') {
				this.setState({ flag: 'add' });
			} else {
				// 点击编辑时可以任意操作基本信息或者菜单授权……并且默认全不展示
				this.setState({
					flag: 'compile',
					locID: userInfo.locID,
					userInfo: userInfo,
					locTypeCode: userInfo.locTypeCode,
				}, () => {
					this.getTableData(this.state.userInfo);
				});
			}
		} else {
			this.setState({ flag: 'add' });
		}
	}

	componentWillUnmount() {
		// 组件销毁前将静止让setState修改state的状态
		this.setState = (state, callback) => { return; }
	}

	componentWillReceiveProps(nextProps) {
		if (
			nextProps.paras && nextProps.paras.userInfo &&
			JSON.stringify(this.state.userInfo) !== JSON.stringify(nextProps.paras.userInfo)
		) {
			this.child.resetFields();
			this.setState({
				userInfo: nextProps.paras.userInfo
			});
		}
	}

	// 新增成功后修改科室locID
	compilelocID = (locID, locTypeCode, obj) => {
		obj.locIPAddress = obj.locAddress ? obj.locAddress : '';
		this.setState({ locID: locID, locTypeCode: locTypeCode, userInfo: obj })
	}

	// 根据id查询单条列表数据
	getTableData(userInfo) {
		let data = {
			params: [{
				locID: userInfo.locID, // 科室名称
				detailFlag: 1,
			}],
			pagination: [{
				pageSize: 10,
				currentPage: 1
			}]
		}
		$http.post('urlS', this, {
			code: "04020008",
			data: data,
			success: function (res) {
				if (+res.errorCode === 0) {
					this.setState({ userInfo: res.result.Data[0] })
				} else {
					this.setState({ userInfo: {} })
				}
			}
		})
	}

	// 导航栏切换
	callback = (key) => {
		this.setState({ activeKey: key })
	}

	// 返回
	goBack = () => {
		this.props.deleteSubMenuTabData(this.props.paras.targetKey, "科室管理");
	}

	handleShowMessage = () => {
		if (this.state.locTypeCode !== 'W') {
			message.info("当科室类型为护理单元时才能操作科室床位")
		}
	}

	handleShowMessageDoctor = () => {
		if (this.state.locTypeCode !== 'O' && this.state.locTypeCode !== 'OI') {
			message.info("当科室类型含门诊时才能操作坐诊医生列表以及医生排班")
		}
	}

	onRef = ref => {
		this.child = ref;
	}

	render() {
		let tabsHeight = store.getState().tableHeight.y + 238 + 'px';
		let { userInfo } = this.state;
		let menuInformation = (
			<div style={{ background: '#f0f2f5' }}>
				<Row style={{ height: '60px', lineHeight: '50px' }}>
					<Col span={24} style={{ height: '50px', background: '#fff', padding: ' 0 24px' }}>
						<span onClick={this.goBack} style={{ cursor: 'pointer', color: '#42b983' }}>
							<Icon type='left' />
							<span>科室管理</span>
						</span>
						<span style={{ margin: '0 10px 0 30px' }}>{userInfo && userInfo.locCode ? userInfo.locCode : '--'}</span>
						<Divider type="vertical" />
						<span style={{ margin: '0 10px' }}>{userInfo && userInfo.locDesc ? userInfo.locDesc : '--'}</span>
						<Divider type="vertical" />
						<span style={{ margin: '0 10px' }}>{userInfo && userInfo.locIPAddress ? userInfo.locIPAddress : (userInfo && userInfo.locOPAddress ? userInfo.locOPAddress : '--')}</span>
					</Col>
				</Row>
			</div>
		)
		return (
			<div className="add-department">
				<Tabs
					activeKey={this.state.activeKey}
					onChange={this.callback}
					tabPosition="left"
					type="card"
					className="new_department_tabs"
					style={{ width: '100%', height: tabsHeight }}
				>
					<TabPane tab="科室基本信息" key="1">
						{menuInformation}
						<DepartmentBasicInformation handleThis={this} userInfo={this.state.userInfo} flag={this.state.flag} onRef={this.onRef} />
					</TabPane>
					{/* 当locTypeCode===W时并且this.state.locID === ""可操作，不等于的时候不可操作 */}
					<TabPane tab={<div onClick={this.handleShowMessage}>科室床位</div>} key="2" disabled={this.state.locTypeCode !== 'W'}>
						{menuInformation}
						<DepartmentFind locID={this.state.locID} />
					</TabPane>
					{this.state.locTypeCode !== 'W' ? <TabPane tab="病区对照" key="3" disabled={this.state.locID === ""}>
						{menuInformation}
						<WardContrst locID={this.state.locID} />
					</TabPane> : ''}
					<TabPane tab="向外院开单项目目录" key="4" disabled={this.state.locID === ""}>
						<ExternalDepartments locID={this.state.locID} />
					</TabPane>
					<TabPane tab="科室专长维护" key="5" disabled={this.state.locID === ""}>
						{menuInformation}
						<DepartmentExpertise locID={this.state.locID} />
					</TabPane>
					<TabPane tab="科室人员维护" key="6" disabled={this.state.locID === ""}>
						{menuInformation}
						{this.props.showSunTabPane ? null : <OfficeStaff locID={this.state.locID} location={this.props} />}
					</TabPane>
					<TabPane tab={<div onClick={this.handleShowMessageDoctor}>坐诊医生列表</div>} key="7" disabled={this.state.locTypeCode !== 'O' && this.state.locTypeCode !== 'OI'}>
						{menuInformation}
						<DoctorList locID={this.state.locID} />
					</TabPane>
					<TabPane tab={<div onClick={this.handleShowMessageDoctor}>医生排班</div>} key="8" disabled={this.state.locTypeCode !== 'O' && this.state.locTypeCode !== 'OI'}>
						{menuInformation}
						<DoctorScheduling locID={this.state.locID} />
					</TabPane>
					{/* 2019-11-06 ML 科室挂号医生对照*/}
					<TabPane tab="科室挂号医生对照" key="9" disabled={this.state.locID === ""}>
						{menuInformation}
						<DepartmentContrastDoctor locID={this.state.locID} ></DepartmentContrastDoctor>
					</TabPane>
					<TabPane tab="病区信息设置" key="10" disabled={this.state.locTypeCode !== 'W'}>
						{menuInformation}
						<WardInformationSettings tabHeight={store.getState().tableHeight.y + 22} locID={this.state.locID} />
					</TabPane>
				</Tabs>
			</div>
		)
	}
}